/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using Gtk;

namespace Whitemice.ZOGI.GtkWidgets
{	
	public abstract class CompactNodeView : Gtk.NodeView, IEntityView
	{
		protected bool	   popupEnabled;
		protected Label	   statusLabel;
		protected int      sortColumn;
		protected SortType sortOrder;
		
		public CompactNodeView(
			Label status, 
			bool popup, 
			Gtk.NodeStore nodeStore)
			: base(nodeStore)
		{
			statusLabel = status;
			popupEnabled = popup;
			//(nodeStore as TreeModelSort).SetSortFunc(-2, OrderEntries);
		}
		
		public virtual int OrderEntries(TreeModel model, TreeIter t1, TreeIter t2)
		{ 
	    Console.WriteLine("CompactNodeView.OrderEntries");
		  return 0;
		}
		
		public bool PopupEnabled
		{
			get { return popupEnabled; }
			set { popupEnabled = value; }
		}
		
		public void AppendTextColumn(string header, int column, 
			bool clickable, bool expand, bool sortable)
		{
			TreeViewColumn tvc;
			tvc = new TreeViewColumn(header, new Gtk.CellRendererText(), "text", column);
			tvc.Clickable = clickable;
			tvc.Expand = expand;
			tvc.Reorderable = true;
			tvc.Resizable = true;
			if (sortable) 
			{
				tvc.SortColumnId = column;
				tvc.SortIndicator = true;
			}
			tvc.Clicked += new EventHandler(ColumnClicked);
			this.AppendColumn(tvc);		
		}
		
		protected void ColumnClicked(object sender, EventArgs args)
		{
		  TreeViewColumn column;

		  if (sender is Gtk.TreeViewColumn)
		  {
		    column = (sender as Gtk.TreeViewColumn);
		    if (column.SortColumnId == sortColumn)
		    {
		      if (sortOrder == SortType.Ascending)
		        sortOrder = SortType.Descending;
		      else sortOrder = SortType.Ascending;
		      column.SortOrder = sortOrder;
		      column.SortIndicator = true;
		    } else 
		      {
		        sortColumn = column.SortColumnId;
		        sortOrder = SortType.Ascending;
		        column.SortIndicator = true;
		      }
		    Console.WriteLine("Sort Column:{0} Order:{1}", sortColumn, sortOrder);
		    //(taskStore as Gtk.TreeSortable).SetSortColumnId(sortColumn, sortOrder);
		  }
		} // end ColumnClicked		
		
		public void Fill(IEnumerable<Whitemice.ZOGI.Entity> data)
		{
			Fill(data, null);
		}
		
		public abstract void Fill(IEnumerable<Entity> data, string message);
		
		public virtual void Remove(Entity _entity)
		{
		  foreach(ITreeNode node in this.NodeStore)
		  {
		    if (node is ZOGITreeNode)
		      if ((node as ZOGITreeNode).ObjectId == _entity.ObjectId)
		      {
            Registry.UnregisterForNotification(_entity, this);
		        NodeStore.RemoveNode(node);
		        return;
		      } 
		  } /* End foreach */
		} /* End Remove */

    public void ReceiveEvent(ServerEvent _event)
    {
      if (_event.Event == ServerEventType.Delete)
        Application.Invoke(delegate { this.Remove(_event.Entity); } );
    }
		
		public virtual void RemoveEvent(Entity _entity, object _context, Exception _exception)
		{
		  if (_entity != null)
		    Remove(_entity);
		} /* End Remove */		
	}
}